<template>
  <el-dialog
    :title="isEdit ? '编辑法律法规' : '新增法律法规'"
    :model-value="visible"
    width="600px"
    :close-on-click-modal="false"
    @close="handleCancel"
    @update:model-value="emit('update:visible', $event)"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="formRef"
      label-width="140px"
      label-position="right"
      class="law-modal-form"
    >
      <el-form-item label="法律法规名称" prop="lawName">
        <el-input v-model="form.lawName" placeholder="请输入文字" />
      </el-form-item>

      <el-form-item label="法条数" prop="rulesNumber">
        <el-input v-model="form.rulesNumber" placeholder="请输入法条数" />
      </el-form-item>

      <el-form-item label="生效日期" prop="failureTime">
        <el-date-picker
          v-model="form.failureTime"
          type="date"
          placeholder="选择日期"
          style="width: 100%"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD hh:mm:ss"
        />
      </el-form-item>
      <el-form-item label="法条内容" prop="responsibilities">
        <el-input
          v-model="form.responsibilities"
          type="textarea"
          :rows="5"
          placeholder="请输入内容"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk" :loading="submitLoading"
          >确认</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch, defineProps, defineEmits } from "vue";
import { createLaw, editLaw } from "./services";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import { ElMessage } from "element-plus";

dayjs.extend(utc);

const props = defineProps({
  visible: Boolean,
  isEdit: Boolean,
  data: {
    type: Object,
    default: () => ({}),
  },
});
const emit = defineEmits(["update:visible", "submit"]);

const formRef = ref();
const form = ref({
  lawName: "",
  failureTime: "",
  responsibilities: "",
  rulesNumber: "",
});
const rules = {
  lawName: [{ required: true, message: "请输入法律法规名称", trigger: "blur" }],
  rulesNumber: [{ required: true, message: "请输入法条数", trigger: "blur" }],
  failureTime: [
    { required: true, message: "请选择生效日期", trigger: "change" },
  ],
  responsibilities: [
    { required: true, message: "请输入法条内容", trigger: "blur" },
  ],
};

const submitLoading = ref(false);

watch(
  () => props.visible,
  (val) => {
    if (val) {
      if (props.isEdit && props.data) {
        console.log(1122, props.data);

        form.value = {
          ...props.data,
          failureTime: props.data.failureTime
            ? dayjs(props.data.failureTime).format("YYYY-MM-DD HH:mm:ss")
            : "",
        };
      } else {
        form.value = {
          lawName: "",
          failureTime: "",
          responsibilities: "",
          rulesNumber: "",
        };
      }
    }
  }
);

function handleCancel() {
  if (formRef.value) {
    formRef.value.resetFields();
  }
  emit("update:visible", false);
}

async function handleOk() {
  submitLoading.value = true;
  formRef.value.validate(async (valid) => {
    if (valid) {
      const payload = {
        ...form.value,
      };

      try {
        const res = props.isEdit
          ? await editLaw(payload)
          : await createLaw(payload);
        if (res.code === 200) {
          ElMessage.success("新增成功");
          emit("submit", payload);
          emit("update:visible", false);
        } else {
          ElMessage.error(res.message);
        }
      } catch (error) {
        ElMessage.error(error.message);
      }
    }
    submitLoading.value = false;
  });
}
</script>

<style scoped>
.law-modal-form {
  margin-top: 10px;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 24px;
  padding-top: 10px;
}
.el-form-item {
  margin-bottom: 18px;
}
.el-input,
.el-textarea,
.el-date-editor {
  width: 100%;
}

:deep(.el-form-item--default) {
  margin-bottom: 18px !important;
}
</style>
